package com.peng._05mvcboot03.controller;

import com.peng._05mvcboot03.common.response.JsonResult;
import com.peng._05mvcboot03.common.response.StatusCode;
import com.peng._05mvcboot03.mapper.ArticleMapper;
import com.peng._05mvcboot03.mapper.UserMapper;
import com.peng._05mvcboot03.pojo.dto.UserArticleDTO;
import com.peng._05mvcboot03.pojo.dto.UserLoginDTO;
import com.peng._05mvcboot03.pojo.entity.Article;
import com.peng._05mvcboot03.pojo.vo.ArticleVO;
import com.peng._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

/**
 * @Author peng
 * @Date 2024-04-26 17:08
 */
@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags = "用户管理")
@Validated
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonResult login(@Validated @RequestBody UserLoginDTO userLoginDTO, HttpSession httpSession) {
        log.info("userLoginDTO:{}", userLoginDTO);
        UserVO userVO = userMapper.selectUserByExample(userLoginDTO);
        if (userVO == null) {
            return JsonResult.error(StatusCode.USERNAME_PASSWORD_ERROR, "登录失败");
        } else {
            httpSession.setAttribute("user", userVO);
            return JsonResult.success("登录成功");
        }
    }

    @PostMapping("/article")
    @ApiOperation("发布文章")
    public JsonResult publishArticle(@Validated @RequestBody UserArticleDTO userArticleDTO, HttpSession httpSession) {
        log.info("userArticleDTO:{}", userArticleDTO);
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        Article article = Article.builder()
                .title(userArticleDTO.getTitle())
                .content(userArticleDTO.getContent())
                .userId(userVO.getId())
                .build();
        int i = articleMapper.insertArticle(article);
        if (i > 0) {
            return JsonResult.success("发布成功");
        } else {
            return JsonResult.error("发布失败");
        }
    }

    @GetMapping("/selectByArticleId")
    @ApiOperation("根据文章ID查询文章")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, paramType = "query", dataType = "Long")
    public JsonResult getArticleById(@Range(min = 1, max = 100, message = "文章ID在1~100之间")
                                     @NotNull(message = "文章ID不能为空")
                                     @RequestParam Long id) {
        ArticleVO articleVO = articleMapper.getArticleById(id);
        if (articleVO == null) {
            return JsonResult.error("文章不存在");
        } else {
            return JsonResult.success("文章查询成功", articleVO);
        }
    }

}
